<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    protected $msg = [
        'nickname.required' => '昵称不能为空',
        'nickname.string' => '昵称必须为字符串',
        'username.required' => '用户名不能为空',
        'username.string' => '用户名必须为字符串',
        'username.unique' => '用户名已存在',
        'mobile.required' => '手机号不能为空',
        'mobile.unique'   => '手机号已存在'
    ];

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return $this->scene();
    }

    public function messages()
    {
        return $this->msg;
    }

    protected function scene()
    {
        $id = $this->route('id');
        $method = $this->method();
        if ($method == 'POST') {
            return [
                'nickname' => 'required|string|max:30',
                'username' => 'required|string|max:32|unique:admin_users',
                'password' => 'required|min:6',
                'mobile'   => 'required|unique:admin_users'
            ];
        } else if ($method == 'PUT') {

            unset($this->msg['password.required']);
            unset($this->msg['password.min']);
            return [
                'nickname' => 'required|string|max:50',
                'username' => 'required|string|max:32|unique:admin_users,username,'.$id,
                'password' => 'min:6',
                'mobile'   => 'required|unique:admin_users,mobile,'.$id
            ];
        }
    }
}
